/**
 * 
 */
package org.hotpotmaterial.codegenn2.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.hotpotmaterial.codegenn2.config.bean.PluginMapProperties;
import org.hotpotmaterial.codegenn2.config.bean.PluginProperties;
import org.hotpotmaterial.codegenn2.exception.CodeCommonException;
import org.hotpotmaterial.codegenn2.service.IPluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;

import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author causer
 *
 */
@Service
@Slf4j
public class PluginServiceImpl implements IPluginService {

	@Autowired
	private PluginMapProperties pluginMapProperties;

	@Override
	public String isDirectory(String pluginName, String dir) {
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		// 获取插件模板目录
		String templateDir = plugin.getPluginTemplate();
		// 读取文件
		File file = new File(templateDir + dir);

		return file.isDirectory() ? "1" : "0";
	}

	@Override
	public List<String> getTemplateFileList(String pluginName, String dir) {
		// 初始化文件列表
		List<String> fileNames = Lists.newArrayList();
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		// 获取插件模板目录
		String templateDir = plugin.getPluginTemplate();
		// 读取文件
		File file = new File(templateDir + dir);
		if (file.isDirectory()) {
			// 如果是目录则便利
			File[] fs = file.listFiles();
			for (File f : fs) {
				fileNames.add(f.getName());
			}
		}
		return fileNames;
	}

	@Override
	public String getFileContent(String pluginName, String fileName) {
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		// 获取插件模板目录
		String templateDir = plugin.getPluginTemplate();
		File file = new File(templateDir + fileName);
		StringBuilder sb = new StringBuilder();
		try (FileReader reader = new FileReader(file); BufferedReader bReader = new BufferedReader(reader);) {
			String s = "";
			while ((s = bReader.readLine()) != null) {
				sb.append(s + "\n");
			}
		} catch (IOException e) {
			log.error("File read failed!", e);
			throw new CodeCommonException(e.getMessage());
		}
		return sb.toString();
	}

	@Override
	public PluginProperties getPluginProperties(String pluginName) {
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		return plugin;
	}

	@Override
	public void updateTemplate(String pluginName, String fileName, String content) {
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		// 获取插件模板目录
		String templateDir = plugin.getPluginTemplate();
		// 更新内容
		FileUtil.writeUtf8String(content, templateDir + fileName);
	}

	@Override
	public void uploadTemplate(String pluginName, String fileName, MultipartFile file) {
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		// 获取插件模板目录
		String templateDir = plugin.getPluginTemplate();
		// 保存文件到硬盘
		try {
			FileUtil.writeFromStream(file.getInputStream(), templateDir + fileName);
		} catch (IORuntimeException | IOException e) {
			log.error("Upload file failed!", e);
			throw new CodeCommonException(e.getMessage());
		}
	}

	@Override
	public void uploadAndUnzipTemplates(String pluginName, MultipartFile file) {
		// 获取插件
		PluginProperties plugin = pluginMapProperties.getProperties().get(pluginName);
		// 获取插件模板目录
		String templateDir = plugin.getPluginTemplate();
		// 解压
		try {
			ZipUtil.unzip(file.getInputStream(), new File(templateDir), Charset.forName("UTF-8"));
		} catch (UtilException | IOException e) {
			log.error("Upload and unzip file failed!", e);
			throw new CodeCommonException(e.getMessage());
		}
	}

}
